// pages/shopInfo/shopInfo.js
var common = require('../../utils/util')
const app = getApp()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        goodInfo: null,
        collect: false,
        url: "../../asset/img/collect.png",
        show: false,
        sum: 1,
        login: false,
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        if (options) {
            if (options.id) {
                this.initialize(options.id)
            }
        }
        this.setData({
            baseUrl: common.baseUrl
        })
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        let array = wx.getStorageSync('carList') || []
        this.setData({
            car_sum:array.length
        })
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    initialize(id) {
        common.http('app/shop/goods/info/' + id, 'GET', (res) => {
            if (res) {
                this.setData({
                    goodInfo: res,
                })
            }
        })
    },

    toggleCollect(e) {
        let collect = !e.currentTarget.dataset.collect
        let url = this.data.url
        if (this.data.collect) {
            url = "../../asset/img/collect.png"
            common.showToast('取消收藏')
        } else {
            url = "../../asset/img/collect_on.png"
            common.showToast('收藏成功')
        }
        this.setData({
            url: url,
            collect: collect
        })
    },

    //取消登录
    handleLoginClose() {
        this.setData({
            login: false
        })
    },
    //登录
    getPhoneNumber(e) {
        if (e.detail) {
            common.handleLogin(e.detail);
        }
        this.setData({
            login: false
        })
    },

    //登录弹窗
    open: function () {
        var token = wx.getStorageSync('token') || null
        if (token) {
            this.setData({
                show: true
            })
        } else {
            this.setData({
                login: true
            })
        }
    },

    decrease() {
        let sum = parseInt(this.data.sum)
        if (sum > 1) {
            this.setData({
                sum: sum - 1,
            })
        }
    },

    add() {
        let goodInfo = this.data.goodInfo
        let sum = parseInt(this.data.sum)
        if (goodInfo && goodInfo.quantity) {
            let quantity = parseInt(goodInfo.quantity);
            if (sum < quantity) {
                this.setData({
                    sum: sum + 1,
                })
            } else {
                common.showToast('没有库存了！')
            }
        }
    },

    //关闭购物弹窗
    handleClose() {
        this.setData({
            show: false
        })
    },

    handleAdd() {
        let array = wx.getStorageSync('carList') || []
        let info = this.data.goodInfo
        info.account = this.data.sum
        if(array.length > 0){
            let ishas = false;
            array.forEach(function(item,index,array){
                if(info.goods_id == item.goods_id){
                    info.account = item.account + info.account
                    array[index] = info
                    ishas = true
                }
            })
            if(!ishas){
                array.push(info)
            }
        }else{
            array.push(info)
        }
        var that = this
        wx.setStorage({
            key:"carList",
            data:array,
            success(){
                common.showToast('加入购物车成功')
                that.setData({
                    car_sum:array.length
                })
            }
          })
    },

    handleGoCar() {
        wx.switchTab({
            url: '../car/car',
        })
    },

    handleNow() {
        let id = this.data.goodInfo.goods_id
        let quantity = this.data.sum
        wx.navigateTo({
          url: '../settle/settle?id=' + id + '&quantity=' + quantity,
        })
    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})